package com.nanyang.tms.api.dto.request.storedrule;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import com.nanyang.tms.common.util.CheckUtils;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * <p>Description:(储值规则表)</p>
 *
 * @author lv
 * @date 2022年11月12日 下午11:01:18
 * @Copyright Copyright (c) 2020 www.nanyang.cn Inc. All rights reserved.
 */
@Data
public class StoredRuleUpdateRequest extends ApiRequest {
    @JsonIgnore
    private String pin;
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 储值规则编码
     */
    @NotEmpty(message = "储值规则编码不能为空")
    @Length(max = 32, message = "ruleCode长度需小于32个字符")
    private String ruleCode;
    /**
     * 储值规则名称
     */
    @NotEmpty(message = "储值规则名称不能为空")
    @Length(max = 50, message = "ruleName长度需小于50个字符")
    private String ruleName;

    /**
     * 最低充值余额
     */
    @NotNull(message = "最低充值余额不能为空")
    @Digits(integer = 9, fraction=2, message = "lowPrice格式不正确")
    @DecimalMin(value = "0.00", message = "lowPrice格式不正确")
    private BigDecimal lowPrice;

    /**
     * 储值金额
     */
    @NotEmpty(message = "储值金额不能为空")
    @Length(max = 100, message = "price长度需小于100个字符")
    private String price;

    /**
     * 类型编码
     */
    @NotEmpty(message = "类型编码不能为空")
    @Length(max = 20, message = "typeCode长度需小于20个字符")
    private String typeCode;

    /**
     * 类型名称（余额、卡）
     */
    @NotEmpty(message = "类型名称不能为空")
    @Length(max = 30, message = "typeName长度需小于30个字符")
    private String typeName;

    @Override
    public void requestCheck() {
        String[] split = price.split(",");
        for (String s : split) {
            if (!CheckUtils.checkAmount(s)) {
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "储值金额格式不正确!");
            }
        }
    }
}